<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改套餐')" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-setmeal-edit" th:object="${aSetmeal}">
        <input name="id" th:field="*{id}" type="hidden">
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label">套餐名：</label>
                <div class="col-sm-8">
                    <input name="setmealName" th:field="*{setmealName}" class="form-control" type="text">
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">套餐分类：</label>
                <div class="col-sm-8">
                    <select name="categoryId" id="setMealCategory" class="form-control" required>
                        <!-- 分类选项将通过 JavaScript 动态填充 -->
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">包含商品：</label>
                <div class="col-sm-8">
                    <select name="productId" id="includProduct" class="form-control" multiple required>
                        <!-- 商品选项将通过 JavaScript 动态填充 -->
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label">图片：</label>
                <!--                 onclick="$.triggerFileInput()"-->
                <div class="UploadImg">  <!-- 为整个区域添加点击事件 -->
                    <div class="imgList" id="imgList">
                        <!-- 这里用于显示已上传的图片或加载状态 -->
                        <img th:if="${aSetmeal.imgUrl}" th:src="${aSetmeal.imgUrl}" width="100" height="100" border-radius="10" border="1px solid #d9d9d9" />
                    </div>
<!--                    <div class="addBtn" id="addBtn" >-->
<!--                        <i class="fa fa-plus"></i>-->
<!--                    </div>-->
                </div>
                <!--                 onchange="$.uploadImage()"-->
                <input type="file" id="fileInput" style="display: none;"/>
                <!-- 隐藏的文件输入元素，并绑定 onchange 事件 -->
                <input name="imgUrl" id="imgUrl" th:field="*{imgUrl}" class="form-control" type="hidden">
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">套餐价格：</label>
                <div class="col-sm-8">
                    <input name="setmealPrice" th:field="*{setmealPrice}" class="form-control" type="text" required>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">积分：</label>
                <div class="col-sm-8">
                    <input name="socre" th:field="*{socre}" class="form-control" type="text" required>
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var prefix = ctx + "system/setmeal";
    var uploadUrl = ctx + "common/upload";
    var categoryUrl = ctx + "system/category/list";
    var producturl = ctx + "system/product/list";

    $(document).ready(function() {
        loadCategories();
        loadProducts();
        $("#form-setmeal-edit").validate({
            focusCleanup: true
        });

        $.fileUpload.init();
    });

    function loadProducts() {
        $.ajax({
            url: producturl,
            type: 'POST',
            dataType: 'json',
            success: function(data) {
                var $productSelect = $('#includProduct');
                $productSelect.empty();
                if (data && data.rows && Array.isArray(data.rows)) {
                    data.rows.forEach(function(product) {
                        var option = $('<option></option>')
                            .attr('value', product.id)
                            .text(product.productName);
                        $productSelect.append(option);
                    });
                }
            },
            error: function(xhr, status, error) {
                console.error('加载商品数据失败:', error);
            }
        });
    }

    function loadCategories() {
        $.ajax({
            url: categoryUrl,
            type: 'POST',
            dataType: 'json',
            success: function(data) {
                var $categorySelect = $('#setMealCategory');
                $categorySelect.empty();
                if (data && data.rows && Array.isArray(data.rows)) {
                    data.rows.forEach(function(category) {
                        var option = $('<option></option>')
                            .attr('value', category.id)
                            .text(category.name);
                        $categorySelect.append(option);
                    });
                }
            },
            error: function(xhr, status, error) {
                console.error('加载分类数据失败:', error);
            }
        });
    }

    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/edit", $('#form-setmeal-edit').serialize());
        }
    }
</script>
</body>
</html>